如果進制為k的n的所有位數均為1,則稱k為n的Good base。
給定一個以string儲存的整數n,求 Smallest Good Base。
uint64_t ull_n = stoull(n);
uint64_t min = 2
uint64_t max = log2(ull_n) + 1
for(uint64_t i = max; i >= min; i--){
uint64_t k = pow(ull_n, 1.0/i); //
if(k < 2){
continue;
}
uint64_t sum = 0;
uint64_t prod = 1;
for(uint64_t j = 1; j <= i + 1; j++){
sum += prod;
prod *= k;
}
if(sum == ull_n){
return to_string(k);
}
}
class Solution {
public:
string smallestGoodBase(string n) {
uint64_t ull_n = stoull(n);
uint64_t min = 2;
uint64_t max = log2(ull_n) + 1;
for(uint64_t i = max; i >= min; i--){
uint64_t k = pow(ull_n, 1.0/i); //
if(k < 2){
continue;
}
uint64_t sum = 0;
uint64_t prod = 1;
for(uint64_t j = 1; j <= i + 1; j++){
sum += prod;
prod *= k;
}
if(sum == ull_n){
return to_string(k);
}
}
return to_string(ull_n - 1);
}
};